{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 百度智能云"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## .检测人脸"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'refresh_token': '25.4c45276bd040b6d0f02230eaf7786f09.315360000.1918880482.282335-22867992', 'expires_in': 2592000, 'session_key': '9mzdXqbP/ww2vmwxmwNI/K96FqWVhVmfqIdZ2QiLWMOGVQh1HquATnA67JQjckbvpi4bGA0k+84qbzA75D7x+fXR8M+JJg==', 'access_token': '24.ec30c4b9553b35b4d23f6b6018ba0800.2592000.1606112482.282335-22867992', 'scope': 'public brain_all_scope vis-faceverify_faceverify_h5-face-liveness vis-faceverify_FACE_V3 vis-faceverify_idl_face_merge vis-faceverify_FACE_EFFECT vis-faceverify_face_feature_sdk wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component', 'session_secret': '77e919a9a6693280e1435c91b0c7d40d'}\n"
     ]
    }
   ],
   "source": [
    "host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={}&client_secret={}'\n",
    "apikey = \"gzrrKaNlQ9GSH3dX54Gved51\"\n",
    "secretkey = \"rxVFw6etVdS4XzDP2lhXnqEynVWKf8vD\"\n",
    "response = requests.get(host.format(apikey, secretkey))\n",
    "if response:\n",
    "    print(response.json())\n",
    "# {'refresh_token': '25.28e474eb76b22b468b28bd214b4c5f5e.315360000.1918366387.282335-22838595',\n",
    "#  'expires_in': 2592000,\n",
    "#  'session_key': '9mzdDcZhxI0DqtPikwQHIKEoKgdnFBrsLrnaN0mbUqjzpTf/BQLtOGJhjMAbUmQN3fXWr8Bp4hkjIbggmsTDGBTb0MAaIQ==',\n",
    "#  'access_token': '24.60f9411a4b4ea9f169195953c398a3e0.2592000.1605598387.282335-22838595',\n",
    "#  'scope': 'public brain_all_scope vis-faceverify_faceverify_h5-face-liveness vis-faceverify_FACE_V3 vis-faceverify_idl_face_merge vis-faceverify_FACE_EFFECT wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component',\n",
    "#  'session_secret': '3467d4c7825a546093e81c2d4249db92'}\n",
    "\n",
    "# 1.人脸检测与属性分析\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/detect\"\n",
    "\n",
    "params = \"{\\\"image\\\":\\\"http://img.idol001.com/origin/2017/11/18/83731b5eae305e8e9dc4ee88660015911510974229_watermark.jpg\\\",\\\"image_type\\\":\\\"URL\\\",\\\"face_field\\\":\\\"faceshape,facetype\\\"}\"\n",
    "# image_type 图片类型：\n",
    "access_token = '24.6d684a22feb8384590448aa0f4edcb8e.2592000.1605596154.282335-22838595' # 调用鉴权接口获取的token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/json'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'error_code': 0,\n",
       " 'error_msg': 'SUCCESS',\n",
       " 'log_id': 1019494001201,\n",
       " 'timestamp': 1603520482,\n",
       " 'cached': 0,\n",
       " 'result': {'face_num': 1,\n",
       "  'face_list': [{'face_token': 'ea408896855b398717c989bc1db786e7',\n",
       "    'location': {'left': 252.92,\n",
       "     'top': 266.62,\n",
       "     'width': 405,\n",
       "     'height': 398,\n",
       "     'rotation': -9},\n",
       "    'face_probability': 1,\n",
       "    'angle': {'yaw': -17.6, 'pitch': 11.4, 'roll': -8.25},\n",
       "    'face_shape': {'type': 'oval', 'probability': 0.52},\n",
       "    'face_type': {'type': 'cartoon', 'probability': 0.65}}]}}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "response.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## .人脸对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'error_code': 0,\n",
       " 'error_msg': 'SUCCESS',\n",
       " 'log_id': 2515258975556,\n",
       " 'timestamp': 1603472810,\n",
       " 'cached': 0,\n",
       " 'result': {'score': 91.79843903,\n",
       "  'face_list': [{'face_token': 'dd0745813c71dbbf9375c03950385539'},\n",
       "   {'face_token': 'ea408896855b398717c989bc1db786e7'}]}}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/match\"\n",
    "\n",
    "params = \"[{\\\"image\\\": \\\"https://p3.ifengimg.com/2018_50/F14608222D63172539BE6227A32EF057D1AD7D38_w1024_h1312.jpg\\\", \\\"image_type\\\": \\\"URL\\\", \\\"face_type\\\": \\\"CERT\\\", \\\"quality_control\\\": \\\"LOW\\\"}, {\\\"image\\\": \\\"http://img.idol001.com/origin/2017/11/18/83731b5eae305e8e9dc4ee88660015911510974229_watermark.jpg\\\", \\\"image_type\\\": \\\"URL\\\", \\\"face_type\\\": \\\"LIVE\\\", \\\"quality_control\\\": \\\"LOW\\\"}]\"\n",
    "# face_type 人脸的类型 LIVE；IDCARD；WATERMARK；CERT；INFRARED。\n",
    "\n",
    "access_token = '24.6d684a22feb8384590448aa0f4edcb8e.2592000.1605596154.282335-22838595' # 调用鉴权接口获取的token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/json'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "response.json()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
